<?php

/**
 * uniBoard ACP User Groups
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_groups{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_item_sure'] = $appCore -> lang['delete_item_sure'];
		
		// Reorder items?
		if ( $appCore -> _POST( 'do') == 'reorder')
		{
			// Grab new positions
			$new_positions = $appCore -> _POST( 'group_pos');
			
			// New positions are not empty array?
			if ( !empty( $new_positions) && is_array( $new_positions))
			{
				// Reiterate
				foreach ( $new_positions as $group_id => $new_pos)
				{
					// New pos is not array?
					if ( !is_array( $new_pos))
					{
						// Force integers
						settype( $group_id, 'integer');
						settype( $new_pos, 'integer');
						
						// Group ID greater than ZERO?
						if ( $group_id > 0)
						{
							$appCore -> db -> update( array( 'group_pos' => $new_pos), 'user_groups', 'group_id = \'' . $group_id . '\'');
						}
					}
				}
				
				// Kill cache
				class_cache::flushCache( 'user_groups');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_reordered_user_groups');
								
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_groups_reordered'], $appCore -> lang['acp_user_groups']);
			}
		}
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Open form
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=groups'));	
		echo $appCore -> drawHidden( 'do', 'reorder');	
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$appCore -> lang['acp_groups_list_name']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['column_position']
			)
		));
				
		// Select Groups
		$appCore -> db -> query( 'SELECT group_id, group_system, group_name, group_prefix, group_suffix, group_pos
		FROM ' . DB_PREFIX . 'user_groups
		ORDER BY group_pos');
		
		// Parse result
		while ( $group_result = $appCore -> db -> fetch_array())
		{
			// Rank Image
			$rank_image = ( isset($group_result[4][0]) ? '<img src="' . IMAGES_PATH . htmlspecialchars( $group_result[4]) . '" alt="" />' : $appCore -> output -> drawImage( 'pip'));
		
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_big">
					<b>' . $group_result[3] . $appCore -> ifString( htmlspecialchars( $group_result[2])) . $group_result[4] .'</b>
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=edit_group&do=edit&group=' . $group_result[0]) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['edit']) . '</a>
<a href="' . $appCore -> adminLink( 'act=edit_group&do=delete&group=' . $group_result[0]) . '" class="delete_confirm">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>
				<div class="cell left_border right_float width_medium center">
					' . $appCore -> drawTextInput( 'group_pos[' . $group_result[0] . ']', $group_result[5], '90px') . '
				</div>');
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close Form
		echo $appCore -> output -> submitButton( $appCore -> lang['reorder_items']);
		echo $appCore -> output -> closeForm();	
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New Group Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_group'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_group_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_group_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_user_groups'], array( $appCore -> adminLink( 'act=groups') => $appCore -> lang['acp_user_groups']), ob_get_clean());
				
	}

}